package com.grace.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grace.entity.User;
import com.grace.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录方法[同步]
     */
    /*
    @PostMapping("/login")
    public String login(String username, String password) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("password", password);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            //TODO把登陆成功的用户信息存入session中
            return "redirect:/item/list";
        }
        return "redirect:/";
    }
    */

    /**
     * 登录方法[异步]
     */
    @PostMapping("/login")
    @ResponseBody
    public String login(String username, String password, HttpSession session) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("password", password);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            //TODO把登陆成功的用户信息存入session中
            session.setAttribute("user", user);
            return "1";
        }
        return "0";
    }

}
